package rtmp;

import decoder.AudioStreamDecoder;
import user.Receive;
import user.ReceiveGroup;
import util.Common;
import util.MsgType;
import org.apache.log4j.Logger;

import java.util.List;

public class RtmpAudio extends Amf{

    private final static Logger logger = Logger.getLogger(RtmpAudio.class);
    private byte[] keyData;

    public void setAudioData(byte[] message,String path,int timestamp,AudioStreamDecoder audioStreamDecoder) {
        try {
//             audioStreamDecoder.decode(message);
            if (message[1] == 0x00){
                keyData = message;
            }
            List<Receive> list = ReceiveGroup.getChannel(path);
            if (list != null) {
                for (Receive receive : list) {
                    if (receive.ready && receive.keyframe) {
                        if (!receive.playing) {
                            continue;
                        }
                        if (!receive.audioKeyFrame){
                            System.out.println("进来");
//                        int sendTime = timestamp - receive.extTimestampNum * Common.TIMESTAMP_MAX_NUM;
//                        if(sendTime > Common.TIMESTAMP_MAX_NUM){
//                            receive.extTimestampNum = timestamp / Common.TIMESTAMP_MAX_NUM;
//                            sendTime = timestamp;
//                        }
//                        RtmpResponse.sendData(keyData, MsgType.MSG_AUDIO, 1337, receive.receive, sendTime,receive.check_size);

                            if(timestamp < Common.TIMESTAMP_MAX_NUM){
                                RtmpResponse.sendData(keyData, MsgType.MSG_AUDIO, 1337, receive.receive, timestamp,receive.check_size);
                            } else {
                                int sendTime = timestamp - receive.prevTimestamp;
                                if(sendTime >= Common.TIMESTAMP_MAX_NUM){
                                    RtmpResponse.sendData(keyData, MsgType.MSG_AUDIO, 1337, receive.receive, timestamp,receive.check_size);
                                } else {
                                    RtmpResponse.sendDataExtTimestamp(keyData, MsgType.MSG_AUDIO, 1337, receive.receive, sendTime,receive.check_size);
                                }
                            }
                            receive.audioKeyFrame = true;
                        }
//                    int sendTime = timestamp - receive.extTimestampNum * Common.TIMESTAMP_MAX_NUM;
//                    if(sendTime > Common.TIMESTAMP_MAX_NUM){
//                        receive.extTimestampNum = timestamp / Common.TIMESTAMP_MAX_NUM;
//                        sendTime = timestamp;
//                    }
//                    RtmpResponse.sendData(message, MsgType.MSG_AUDIO, 1337, receive.receive, sendTime,receive.check_size);

                        if(timestamp < Common.TIMESTAMP_MAX_NUM){
                            RtmpResponse.sendData(message, MsgType.MSG_AUDIO, 1337, receive.receive, timestamp,receive.check_size);
                        } else {
                            int sendTime = timestamp - receive.prevTimestamp;
                            if(sendTime >= Common.TIMESTAMP_MAX_NUM){
                                RtmpResponse.sendData(message, MsgType.MSG_AUDIO, 1337, receive.receive, timestamp,receive.check_size);
                            } else {
                                RtmpResponse.sendDataExtTimestamp(message, MsgType.MSG_AUDIO, 1337, receive.receive, sendTime,receive.check_size);
                            }
                        }

                        if(timestamp >= Common.TIMESTAMP_MAX_NUM){
                            receive.prevTimestamp = timestamp;
                        }
                    }
                }
            }
        }catch (Exception e) {
            logger.info(e.getMessage());
        }

    }
}
